<template>
  <el-dialog
      title="新增快递员"
      width="600px"
      @close="cancel"
      :close-on-click-modal="false"
      :visible.sync="visible">
    <el-form :model="form" :rules="rules" ref="Form" label-width="140px">
      <el-form-item label="快递员账号" prop="username">
        <el-input v-model.trim="form.username"></el-input>
      </el-form-item>
      <el-form-item label="快递员姓名(中文)" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="快递员姓名(老挝)" prop="laosName">
        <el-input v-model="form.laosName"></el-input>
      </el-form-item>
      <el-form-item label="快递员联系方式" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <submit-button ref="Submit" @submit='submit'/>
    </div>
  </el-dialog>
</template>

<script>
  import {addCourierApi} from '@/api/site/courier'
  import {resetForm} from "@/utils/common";

  export default {
    name: "Add",
    data() {
      return {
        visible: false,
        form: {
          username: '', // 用户名
          name: '', laosName: '', // 名称
          phone: '' // 联系方式
        },
        rules: {
          username: {required: true, message: '请输入账号', trigger: 'blur'},
          name: {required: true, message: '请输入名称', trigger: 'blur'},
          laosPhone: {required: true, message: '请输入联系方式', trigger: 'blur'}
        }
      }
    },
    methods: {
      submit() {
        this.$refs['Form'].validate((valid) => {
          if (!valid) return false;
          let data = {...this.form};
          this.$refs.Submit.start();
          addCourierApi(data).then(() => {
            this.$refs.Submit.stop();
            this.$emit('update');
            this.cancel()
          }).catch(() => {
            this.$refs.Submit.stop();
          });
        });
      },
      cancel() {
        resetForm(this)
      }
    }
  }
</script>
